//
//  JHPreferencesViewController.m
//  JuniRengLish_iOS
//
//  Created by Maiyou on 2019/5/20.
//  Copyright © 2019 revogi. All rights reserved.
//

#import "JHPreferencesViewController.h"
#import "JHPreferencesInfoModel.h"

@interface JHPreferencesViewController ()

@property (nonatomic, strong) UIButton * fontStyleSelectedBtn;
@property (nonatomic, strong) UIButton * fontSizeSelectedBtn;
@property (nonatomic, strong) UIButton * playModeSelectedBtn;
@property (nonatomic, strong) UIButton * timingSelectedBtn;
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet UILabel *currentRate;

@end

@implementation JHPreferencesViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"偏好设置";
    
    [self.slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    NSArray * array = @[[self getFontStyle], [self getFontSize], [self getPlayMode], @"40"];
    for (NSString * string in array)
    {
        UIButton * button = [self.view viewWithTag:string.integerValue];
        button.selected = YES;
        if (button.tag < 20) {
            _fontStyleSelectedBtn = button;
        }else if (button.tag < 30){
            _fontSizeSelectedBtn = button;
        }else if (button.tag < 40){
            _playModeSelectedBtn = button;
        }else if (button.tag < 50){
            _timingSelectedBtn = button;
        }
    }
}

- (void)sliderValueChanged:(UISlider *)slider
{
    self.currentRate.text = [NSString stringWithFormat:@"当前值%.1f", slider.value];
}

- (NSString *)getPlayMode
{
    return [NSString stringWithFormat:@"%ld", (long)[JHPreferencesInfoModel shareInstance].playMode];
}

- (NSString *)getFontStyle
{
    return [NSString stringWithFormat:@"%ld", (long)[JHPreferencesInfoModel shareInstance].fontStyle];
}

- (NSString *)getFontSize
{
    NSString * string = @"21";
    switch ([JHPreferencesInfoModel shareInstance].fontSize)
    {
        case 12:
            string = @"20";
            break;
        case 14:
            string = @"21";
            break;
        case 16:
            string = @"22";
            break;
        case 18:
            string = @"23";
            break;
            
        default:
            break;
    }
    return string;
}

#pragma mark - 字体样式
- (IBAction)selectFontStyleClick:(id)sender
{
    UIButton * button = sender;
    if (self.fontStyleSelectedBtn != button)
    {
        button.selected = YES;
        self.fontStyleSelectedBtn.selected = NO;
        self.fontStyleSelectedBtn = button;
    }
}

#pragma mark - 字体大小
- (IBAction)selectFontSizeClick:(id)sender
{
    UIButton * button = sender;
    if (self.fontSizeSelectedBtn != button)
    {
        button.selected = YES;
        self.fontSizeSelectedBtn.selected = NO;
        self.fontSizeSelectedBtn = button;
    }
}

#pragma mark - 播放语速
- (IBAction)selectPlayModeClick:(id)sender
{
    UIButton * button = sender;
    if (self.fontStyleSelectedBtn != button)
    {
        button.selected = YES;
        self.playModeSelectedBtn.selected = NO;
        self.playModeSelectedBtn = button;
    }
}

#pragma mark - 定时时间
- (IBAction)selectTimingClick:(id)sender
{
    UIButton * button = sender;
    if (self.fontStyleSelectedBtn != button)
    {
        button.selected = YES;
        self.timingSelectedBtn.selected = NO;
        self.timingSelectedBtn = button;
    }
}

#pragma mark - 保存偏好设置
- (IBAction)savePerfencesClick:(id)sender
{
    //字体
    NSInteger fontSize = 14;
    switch (self.fontSizeSelectedBtn.tag) {
        case 20:
            fontSize = 12;
            break;
        case 21:
            fontSize = 14;
            break;
        case 22:
            fontSize = 16;
            break;
        case 23:
            fontSize = 18;
            break;
            
        default:
            break;
    }
    [DJTool saveUserdefultValue:@(fontSize) forKey:@"fontSize"];
    
    //字体样式
    [DJTool saveUserdefultValue:@(self.fontStyleSelectedBtn.tag) forKey:@"fontStyle"];
    
    //播放模式
    [DJTool saveUserdefultValue:@(self.playModeSelectedBtn.tag) forKey:@"playMode"];
    
    //定时时间
    NSInteger time = 0;
    switch (self.fontSizeSelectedBtn.tag) {
        case 40:
            time = 0;
            break;
        case 41:
            time = 15;
            break;
        case 42:
            time = 30;
            break;
        case 43:
            time = 45;
            break;
            
        default:
            break;
    }
    self.preferencesInfo(time, self.slider.value);
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
